<?php

namespace app\service\Admin;

use app\model\Config;
use Exception;
use app\util\GlobalCode;
use app\util\GlobalMsg;
use function Symfony\Component\String\length;

class ConfigService
{

    public static function getList(array $where = [], int $page = 0, int $pageSize = 0)
    {
        $config = new Config();

        if (!empty($where['name'])) {
            $config = $config->where('name','like', '%'.$where['name'].'%');
        }
        if (!empty($where['value'])) {
            $config = $config->where('value', 'like', '%'.$where['value'].'%');
        }

        $count = $config->count();

        if ($page > 0 && $pageSize > 0) {
            $config = $config->forPage($page, $pageSize);
        }

        $list = $config->orderBy('id', 'desc')->get()->toArray();

        return ['count' => $count, 'list' => $list];
    }

    public static function getAll(array $where = [])
    {
        $config = new Config();

        if (!empty($where['type'])) {
            $config = $config->where('type','=', $where['type']);
        }
        if (!empty($where['name'])) {
            $config = $config->where('name','like', '%'.$where['name'].'%');
        }
        if (!empty($where['value'])) {
            $config = $config->where('value', 'like', '%'.$where['value'].'%');
        }

        return $config->orderBy('id', 'desc')->get()->toArray();
    }

    public static function getOne(int $id = 0)
    {
        $config = Config::where('id', $id)->first();
        if ($config == null) {
            throw new Exception(GlobalMsg::GET_HAS_NO);
        }
        return $config;
    }

    public static function add(array $where = [])
    {

        $config = new Config();
        if (!empty($where['id'])) {
            throw new Exception(GlobalMsg::ADD_ID);
        }
        isset($where['type']) && $config->type = $where['type'];
        isset($where['category']) && $config->category = $where['category'];
        isset($where['name']) && $config->name = $where['name'];
        isset($where['key']) && $config->key = $where['key'];
        isset($where['value']) && $config->value = $where['value'];

        $res = $config->save();
        if ($res == false) {
            throw new Exception(GlobalMsg::SAVE_FAIL);
        }
        return $config->id;
    }

    public static function save(array $where = [])
    {
        if (empty($where['id'])) {
            throw new Exception(GlobalMsg::SAVE_NO_ID);
        }
        $config = Config::where('id', $where['id'])->first();
        if ($config == null) {
            throw new Exception(GlobalMsg::SAVE_HAS_NO);
        }
        isset($where['type']) && $config->type = $where['type'];
        isset($where['category']) && $config->category = $where['category'];
        isset($where['name']) && $config->name = $where['name'];
        isset($where['key']) && $config->key = $where['key'];
        isset($where['value']) && $config->value = $where['value'];


        $res = $config->save();
        if ($res == false) {
            throw new Exception(GlobalMsg::SAVE_FAIL);
        }
        return $res;
    }

    public static function settingConfig(array $where = [])
    {
        $type = $where['type'];
        unset($where['type']);

        $configs = Config::where('type',$type )->get()->toArray();

        $oldArr = array();
        $oldList = array();
        foreach ($configs as $item) {
            $oldArr[] = $item['key'];
            $oldList[$item['key']] = $item;
        }

        $newArr = array();
        foreach ($where as $key=>$val){
            $newArr[] = $key;
        }

        $jiao = array_intersect($newArr,$oldArr);//交集，需要修改【新、旧】
        $cha_add = array_diff($newArr,$oldArr);//差级，需要新增【新、旧】
        $cha_del = array_diff($oldArr,$newArr);//差级，需要删除【旧、新】
        if($jiao){
            foreach ($jiao as $key) {
                $item = $oldList[$key];
                $item['value'] = $where[$key];
                (new Config())->where('id',$item['id'])->update($item);
            }
        }
        if ($cha_add) {
            $addList = array();
            foreach ($cha_add as $key) {
                $item = array();
                $item['type'] = $type;
                $item['key'] = $key;
                $item['value'] = $where[$key];
                $addList[] = $item;
            }
            (new Config())->insert($addList);
        }
        if ($cha_del) {
            $delIdArr = array();
            foreach ($cha_del as $key) {
                $delIdArr[] = $oldList[$key]['id'];
            }
            Config::whereIn('id', $delIdArr)->delete();
        }
    }

    public static function delete(int $id = 0)
    {
        $config = Config::where('id', $id)->first();
        if ($config == null) {
            throw new Exception(GlobalMsg::DEL_HAS_NO);
        }
        $res = $config->delete();
        if ($res == false) {
            throw new Exception(GlobalMsg::SAVE_FAIL);
        }
        return $res;
    }
}
